﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmPhieuXuatDC : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private DungCuBOCollection DungCuBOCollection = null;
        private LoaiDungCuBOCollection LoaiDungCuBOCollection = null;
        private PhieuXuatDCBOCollection PhieuXuatDCBOCollection = null;
        private PhieuXuatDCChiTietBOCollection PhieuXuatDCChiTietBOCollection = PhieuXuatDCChiTietBOCollection.NewPhieuXuatDCChiTietBOCollection();
        private PhieuXuatDCBO PhieuXuatDCBO = PhieuXuatDCBO.NewRootPhieuXuatDCBO();

        private string COLUMN_FIRST_DungCu = "Ten";
        private string COLUMN_SECOND_DungCu = "SoThuTu";

        private string COLUMN_FIRST_PhieuXuatDCChiTiet = "Ten";
        private string COLUMN_SECOND_PhieuXuatDCChiTiet = "HeSo";
        #endregion variant

        //=====================================
        #region form
        public FrmPhieuXuatDC()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmPhieuXuatDC_Load);
            this.FormClosing += new FormClosingEventHandler(FrmPhieuXuatDC_FormClosing);
            this.KeyDown += new KeyEventHandler(this.FrmHoadonnhap_KeyDown);

            this.btnAdd.Click += new EventHandler(btnAdd_Click);
            this.btnXoa.Click += new EventHandler(btnDelete_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.btnRefreshDungCu.Click += new EventHandler(btnRefreshDungCu_Click);

            this.btnXuatKho.Click += new EventHandler(toolStripMenuItemNhapKho_Click);
            this.btnDelPhieuNhap.Click += new EventHandler(btnDelete_Click);

            this.grdViewDungCu.DoubleClick += new System.EventHandler(this.grdViewDungCu_DoubleClick);
            this.gridViewPhieuXuatDC.RowCellClick += new RowCellClickEventHandler(this.gridViewPhieuXuatDC_RowCellClick);

            this.grdViewDungCu.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);
            this.grdViewPhieuXuatDCChiTiet.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);

            this.grdViewDungCu.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
            this.grdViewPhieuXuatDCChiTiet.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmPhieuXuatDC_Load(object sender, EventArgs e)
        {
            RefreshData();
            //SetFormStatus(this.formStatus);

            Utility.InitControl(this.reLookUpDungCuID);
            Utility.InitControl(this.reLookUpDonViTinhID);

            Utility.InitControl(this.reLookUpDungCu);
            Utility.InitControl(this.reLookUpDonViTinh);
            Utility.InitControl(this.reLookUpDMKhoID);

            Utility.InitControl(this.reLookUpDonViTinhID);

            Utility.InitControl(this.lookUpEditNhaCungCap);

            Utility.InitControl(this.reLookUpNhaCungCapID);

            this.grdViewDungCu.FocusedRowChanged += new FocusedRowChangedEventHandler(grdViewDungCu_FocusedRowChanged);
            this.grdView.FocusedRowChanged += new FocusedRowChangedEventHandler(grdView_FocusedRowChanged);
            this.txtTimkiem.EditValueChanged += new System.EventHandler(this.txtTimkiem_EditValueChanged);

            this.spinEditDungCumDungCu.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditChietKhau.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditVAT.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditTienPhatSinh.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.grdViewPhieuXuatDCChiTiet.CellValueChanged += new CellValueChangedEventHandler(this.grdViewPhieuXuatDCChiTiet_CellValueChanged);

        }

        private void FrmPhieuXuatDC_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }

        private void FrmHoadonnhap_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyData == Keys.Delete)
            {
                int _idDungCu = StaticUtil.GetInt(this.grdViewPhieuXuatDCChiTiet.GetFocusedRowCellValue("DungCuID"), -1);
                int _idDonViTinh = StaticUtil.GetInt(this.grdViewPhieuXuatDCChiTiet.GetFocusedRowCellValue("DonViTinhID"), -1);
                if ((_idDungCu >= 0) & (_idDonViTinh >= 0))
                {
                    DeleteDungCu(_idDungCu, _idDonViTinh);
                }
                else
                {
                    MessageBox.Show("Bạn chưa chọn Vật tư để xóa! Vui lòng chọn lại", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.grdViewPhieuXuatDCChiTiet.Focus();
                }
            }

        }
        #endregion form

        //=====================================
        #region control

        private void txtTimkiem_EditValueChanged(object sender, EventArgs e)
        {
            FilterDungCuByName(txtTimkiem.Text.Trim());
        }


        private void spinEditTienPhatSinh_ValueChanged(object sender, EventArgs e)
        {
            TinhTien();
        }
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnRefreshDungCu_Click(object sender, EventArgs e)
        {
            FilterDungCuByName("");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNew();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int _PhieuXuatDCID = StaticUtil.GetInt(this.PhieuXuatDCBO.PhieuXuatDCID, -1);
            if (_PhieuXuatDCID != -1)
            {
                if (MessageBox.Show("Bạn muốn xóa Phiếu yêu cầu này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Model.PhieuXuatDCBO.DeletePhieuXuatDCBO(_PhieuXuatDCID);
                    for (int k = 0; k < PhieuXuatDCChiTietBOCollection.Count; k++)
                    {
                        PhieuXuatDCChiTietBO.DeletePhieuXuatDCChiTietBO((int)PhieuXuatDCChiTietBOCollection[k].PhieuXuatDCChiTietID);
                    }
                    RefreshData();
                    btnAdd_Click(e, e);
                }
            }
            else
            {
                MessageBox.Show("Chưa chọn Phiếu yêu cầu để xóa! ", "Thông báo", MessageBoxButtons.OK);
            }
        }
        #endregion button

        //=====================================
        #region grid
        private void grdViewDungCu_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            //FilterDungCu();
        }

        private void grdView_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            FilterDungCu();
        }

        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewDungCu.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }

        private void grdViewDungCu_DoubleClick(object sender, EventArgs e)
        {
            UpdatePhieuXuatDC();
        }

        private void gridViewPhieuXuatDC_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            int PhieuXuatDCID = StaticUtil.GetInt(gridViewPhieuXuatDC.GetFocusedRowCellValue("PhieuXuatDCID"));
            //int kiemtraID = kiemtra(HoadonnhapChitietBOCollection, (int)_id);
            //if (kiemtraID == -1)
            //{
            this.PhieuXuatDCBO = Model.PhieuXuatDCBO.GetRootPhieuXuatDCBO(PhieuXuatDCID);
            this.PhieuXuatDCChiTietBOCollection = Model.PhieuXuatDCChiTietBOCollection.GetPhieuXuatDCChiTietBOCollection("PhieuXuatDCID = " + PhieuXuatDCID.ToString());

            lookUpEditNhaCungCap.EditValue = PhieuXuatDCBO.KhachHangID.Value;
            txtMaSo.Text = PhieuXuatDCBO.MaSo;
            txtChungTu.Text = PhieuXuatDCBO.ChungTu;
            txtNoidung.Text = PhieuXuatDCBO.NoiDung;
            txtGhichu.Text = PhieuXuatDCBO.GhiChu;
            dtNgayLapHoaDon.DateTime = PhieuXuatDCBO.NgayLapPhieu.Value;

            spinEditVAT.Value = (decimal)PhieuXuatDCBO.MucVAT;
            spinEditChietKhau.Value = (decimal)PhieuXuatDCBO.MucChietKhau;
            spinEditTienPhatSinh.Value = (decimal)PhieuXuatDCBO.TienPhatSinh;
            txtLyDoPhatSinh.Text = PhieuXuatDCBO.LyDoPhatSinh;

            bdSourcePhieuXuatDCChiTiet.DataSource = PhieuXuatDCChiTietBOCollection.Clone();

            sapxep();
            TinhTien();
        }

        private void grdViewPhieuXuatDCChiTiet_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                int _idDungCu = StaticUtil.GetInt(this.grdViewPhieuXuatDCChiTiet.GetFocusedRowCellValue("DungCuID"));
                int _idDonViTinh = StaticUtil.GetInt(this.grdViewPhieuXuatDCChiTiet.GetFocusedRowCellValue("DonViTinhID"));
                double _soLuong = StaticUtil.GetDouble(this.grdViewPhieuXuatDCChiTiet.GetFocusedRowCellValue("SoLuong"));
                double _DungCuMua = StaticUtil.GetDouble(this.grdViewPhieuXuatDCChiTiet.GetFocusedRowCellValue("DonGia"));

                int kiemtraID = this.PhieuXuatDCChiTietBOCollection.CheckExits(_idDungCu);
                double thanhtien = (double)_soLuong * (double)_DungCuMua;

                PhieuXuatDCChiTietBOCollection[kiemtraID].SoLuong = _soLuong;
                PhieuXuatDCChiTietBOCollection[kiemtraID].ThanhTien = thanhtien;
                sapxep();
                TinhTien();
            }
            catch { }
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterDungCu()
        {
            Session.LoaiDungCuID = StaticUtil.GetRealInt(this.grdView.GetFocusedRowCellValue("LoaiDungCuID"));

            if (Session.LoaiDungCuID == -1)
            {
                //this.grdViewPhieuXuatDCChiTiet.ActiveFilterString = "DungCuID=" + int.MaxValue;

                this.DungCuBOCollection = DungCuBOCollection.GetDungCuBOCollection();
                this.bdSourceDungCu.DataSource = this.DungCuBOCollection.Clone();
            }
            else
            {
                DungCuBOCollection DungCuBOCollection = DungCuBOCollection.GetDungCuBOCollection("LoaiDungCuID = " + Session.LoaiDungCuID.ToString());
                string strWhere = "";
                string listID = "";
                int recordCount = DungCuBOCollection.Count;
                if (recordCount > 0)
                {
                    for (int i = 0; i < recordCount; i++)
                    {
                        object _idVattu = DungCuBOCollection[i].DungCuID;
                        listID += _idVattu.ToString() + ",";
                    }
                }
                strWhere = strWhere + " DungCuID in ('" + listID.Replace(",", "','") + "')";
                this.DungCuBOCollection = DungCuBOCollection.GetDungCuBOCollection(strWhere);
                this.bdSourceDungCu.DataSource = this.DungCuBOCollection.Clone();
            }

            //this.grdViewPhieuXuatDCChiTiet.ActiveFilterString = "DungCuID=" + int.MaxValue;


            //this.bdSourceDungCu.DataSource = this.DungCuBOCollection.Clone();

            //this.grdViewPhieuXuatDCChiTiet.CheckLoaded();
            //SetSttPhieuXuatDCChiTiet();
        }

        private void FilterDungCuByName(string nameDungCu)
        {
            //this.DungCuBOCollection = DungCuBOCollection.GetDungCuBOCollection("Ten like N'%" + nameDungCu + "%'");
            //this.bdSourceDungCu.DataSource = this.DungCuBOCollection.Clone();
            DungCuBOCollection DungCuBOCollection = DungCuBOCollection.GetDungCuBOCollection("Ten like N'%" + nameDungCu + "%'");
            string strWhere = "";
            string listID = "";
            int recordCount = DungCuBOCollection.Count;
            if (recordCount > 0)
            {
                for (int i = 0; i < recordCount; i++)
                {
                    object _idVattu = DungCuBOCollection[i].DungCuID;
                    listID += _idVattu.ToString() + ",";
                }
            }
            strWhere = strWhere + " DungCuID in ('" + listID.Replace(",", "','") + "')";
            this.DungCuBOCollection = DungCuBOCollection.GetDungCuBOCollection(strWhere);
            this.bdSourceDungCu.DataSource = this.DungCuBOCollection.Clone();
        }

        private void ResetData()
        {
            //NhanVienBOList nhanVienBOList = NhanVienBOList.GetNhanVienBOList();
            //Utility.SetData(this.lookUpNhanVienID, nhanVienBOList, nhanVienBOList.Count);

            //NhaCungCapBOList NhaCungCapBOList = NhaCungCapBOList.GetNhaCungCapBOList();
            //Utility.SetData(this.lookUpEditNhaCungCap, NhaCungCapBOList, NhaCungCapBOList.Count);
            lookUpEditNhaCungCap.EditValue = null;
            txtMaSo.Text = "";
            txtChungTu.Text = "";
            txtGhichu.Text = "";
            txtNguoiNhan.Text = "";
            txtNoidung.Text = "";
            txtLyDoPhatSinh.Text = "";
            spinEditChietKhau.Value = spinEditDungCumDungCu.Value = spinEditVAT.Value = spinEditTienPhatSinh.Value = 0;

            dtNgayLapHoaDon.DateTime = DateTime.Now;
            this.PhieuXuatDCBO = Model.PhieuXuatDCBO.NewRootPhieuXuatDCBO();
            this.PhieuXuatDCChiTietBOCollection = Model.PhieuXuatDCChiTietBOCollection.NewPhieuXuatDCChiTietBOCollection();
            sapxep();
            TinhTien();
            bdSourcePhieuXuatDCChiTiet.DataSource = this.PhieuXuatDCChiTietBOCollection.Clone();
        }

        private void RefreshData()
        {
            ResetData();
            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reLookUpDonViTinh, donViTinhBOList, donViTinhBOList.Count);
            Utility.SetData(this.reLookUpDonViTinhID, donViTinhBOList, donViTinhBOList.Count);

            DanhMucBOList dmKhoBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DanhMucKho);
            Utility.SetData(this.reLookUpDMKhoID, dmKhoBOList, dmKhoBOList.Count);

            DungCuBOList DungCuBOList = DungCuBOList.GetDungCuBOList();
            Utility.SetData(this.reLookUpDungCu, DungCuBOList, DungCuBOList.Count);
            Utility.SetData(this.reLookUpDungCuID, DungCuBOList, DungCuBOList.Count);

            KhachHangBOList nhaCungCapBOList = KhachHangBOList.GetKhachHangBOList("NCC = 1");
            Utility.SetData(this.reLookUpNhaCungCapID, nhaCungCapBOList, nhaCungCapBOList.Count);
            Utility.SetData(this.lookUpEditNhaCungCap, nhaCungCapBOList, nhaCungCapBOList.Count);


            DanhMucBOList phuongThucThanhToanBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.PhuongThucThanhToan);

            //this.bdSourceNhaCungCap.DataSource = NhaCungCapBOList.Clone();
            //this.lookUpNhaCungCapID.Properties.DataSource = NhaCungCapBOList;

            #endregion BOList

            this.DungCuBOCollection = DungCuBOCollection.GetDungCuBOCollection();
            //this.PhieuXuatDCChiTietBOCollection = PhieuXuatDCChiTietBOCollection.GetPhieuXuatDCChiTietBOCollection();
            this.PhieuXuatDCBOCollection = Model.PhieuXuatDCBOCollection.GetPhieuXuatDCBOCollection("ThanhToan = 0 and TinhTrangXuatKho = 0");
            this.LoaiDungCuBOCollection = LoaiDungCuBOCollection.GetLoaiDungCuBOCollection();

            LoaiDungCuBO NewLoaiDungCuBO = LoaiDungCuBO.NewRootLoaiDungCuBO();
            NewLoaiDungCuBO.Ten = "Tất cả";
            NewLoaiDungCuBO.LoaiDungCuID = -1;
            this.LoaiDungCuBOCollection.Add(NewLoaiDungCuBO);

            this.bdSourceDungCu.DataSource = this.DungCuBOCollection.Clone();
            //this.bdSourcePhieuXuatDCChiTiet.DataSource = this.PhieuXuatDCChiTietBOCollection.Clone();
            this.bdSourceLoaiDungCu.DataSource = this.LoaiDungCuBOCollection.Clone();
            this.bdSourcePhieuXuatDC.DataSource = this.PhieuXuatDCBOCollection.Clone();


            FilterDungCu();
        }

        private void Edit()
        {
            this.formStatus = FormStatus.Edit;
            //SetFormStatus(this.formStatus);
            Session.CloseIndex++;
        }

        private void AddNew()
        {
            this.PhieuXuatDCBO = Model.PhieuXuatDCBO.NewRootPhieuXuatDCBO();
            this.PhieuXuatDCChiTietBOCollection = Model.PhieuXuatDCChiTietBOCollection.NewPhieuXuatDCChiTietBOCollection();
            ResetData();
        }

        private void Save()
        {
            if (kiemtraLuu() == 1)
            {
                this.PhieuXuatDCBO.KhachHangID = StaticUtil.GetInt(lookUpEditNhaCungCap.EditValue);
                this.PhieuXuatDCBO.MaSo = txtMaSo.Text.Trim();
                this.PhieuXuatDCBO.ChungTu = txtChungTu.Text.Trim();
                this.PhieuXuatDCBO.NgayLapPhieu = dtNgayLapHoaDon.DateTime;
                this.PhieuXuatDCBO.NoiDung = txtNoidung.Text.Trim();
                this.PhieuXuatDCBO.GhiChu = txtGhichu.Text.Trim();
                this.PhieuXuatDCBO.NguoiNhanHang = txtNguoiNhan.Text.Trim();

                this.PhieuXuatDCBO.MucVAT = StaticUtil.GetDouble(spinEditVAT.Value);
                this.PhieuXuatDCBO.MucChietKhau = StaticUtil.GetDouble(spinEditChietKhau.Value);
                this.PhieuXuatDCBO.MucGiamGia = StaticUtil.GetDouble(spinEditDungCumDungCu.Value);
                this.PhieuXuatDCBO.TienPhatSinh = StaticUtil.GetDouble(spinEditTienPhatSinh.Value);
                this.PhieuXuatDCBO.LyDoPhatSinh = txtLyDoPhatSinh.Text.Trim();

                this.PhieuXuatDCBO.TongSo = StaticUtil.GetDouble(spinEditTongSo.Value);
                this.PhieuXuatDCBO.ThanhTien = StaticUtil.GetDouble(spinEditThanhTien.Value);
                this.PhieuXuatDCBO.TienPhaiThu = StaticUtil.GetDouble(spinEditTongTien.Value);
                this.PhieuXuatDCBO.TienThu = 0;
                this.PhieuXuatDCBO.BangChu = txtBangChu.Text.Trim();
                this.PhieuXuatDCBO.TienThua = this.PhieuXuatDCBO.TienThu.Value - this.PhieuXuatDCBO.TienPhaiThu.Value;
                this.PhieuXuatDCBO.ThanhToan = 0;
                this.PhieuXuatDCBO.TinhTrangXuatKho = 0;

                this.PhieuXuatDCBO.Save();

                int recordCount = this.PhieuXuatDCChiTietBOCollection.Count;
                if (recordCount > 0)
                {
                    for (int i = 0; i < recordCount; i++)
                    {
                        this.PhieuXuatDCChiTietBOCollection[i].PhieuXuatDCID = this.PhieuXuatDCBO.PhieuXuatDCID.Value;
                    }

                    this.PhieuXuatDCChiTietBOCollection.Save();
                }

                MessageBox.Show("Cập nhật thành công! ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.PhieuXuatDCChiTietBOCollection = this.PhieuXuatDCChiTietBOCollection.Save();
                this.PhieuXuatDCBOCollection = Model.PhieuXuatDCBOCollection.GetPhieuXuatDCBOCollection("ThanhToan = 0 and TinhTrangXuatKho = 0");
                this.bdSourcePhieuXuatDC.DataSource = this.PhieuXuatDCBOCollection.Clone();
                this.bdSourcePhieuXuatDCChiTiet.DataSource = this.PhieuXuatDCChiTietBOCollection.Clone();
            }


            FilterDungCu();
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            //SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            this.bdSourceDungCu.DataSource = this.DungCuBOCollection.Clone();
            this.bdSourcePhieuXuatDCChiTiet.DataSource = this.PhieuXuatDCChiTietBOCollection.Clone();

            FilterDungCu();
        }

        private void UpdatePhieuXuatDC()
        {
            int DungCuID = StaticUtil.GetInt(grdViewDungCu.GetFocusedRowCellValue("DungCuID"));
            int donViTinhID = StaticUtil.GetInt(grdViewDungCu.GetFocusedRowCellValue("IDDonvitinh"));
            double GiaMua = StaticUtil.GetDouble(grdViewDungCu.GetFocusedRowCellValue("GiaBan"));
            int kiemtraID = this.PhieuXuatDCChiTietBOCollection.CheckExits(DungCuID);
            if (kiemtraID == -1)
            {
                PhieuXuatDCChiTietBO newBO = PhieuXuatDCChiTietBO.NewRootPhieuXuatDCChiTietBO();
                newBO.DungCuID = DungCuID;
                newBO.TenDungCu = DungCuBO.GetRootDungCuBO(DungCuID).Ten;
                newBO.DonViTinhID = donViTinhID;
                newBO.TenDonViTinh = DanhMucBO.GetRootDanhMucBO(donViTinhID).NoiDung;
                newBO.DonGia = GiaMua;
                newBO.SoLuong = 1;
                newBO.ThanhTien = GiaMua * newBO.SoLuong.Value;
                PhieuXuatDCChiTietBOCollection.Add(newBO);

                sapxep();
                TinhTien();
            }
            else
                MessageBox.Show("Mặt hàng và Đơn vị tính này đã có! ", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);

        }

        private void sapxep()
        {

            int length = PhieuXuatDCChiTietBOCollection.Count;

            for (int i = 0; i < length; i++)
            {
                PhieuXuatDCChiTietBOCollection[i].STT0 = (i + 1);
            }

            bdSourcePhieuXuatDCChiTiet.DataSource = PhieuXuatDCChiTietBOCollection;
        }

        private void TinhTien()
        {
            double tongso = 0;
            double thanhtien = 0;
            double tongtien = 0;

            int length = PhieuXuatDCChiTietBOCollection.Count;

            for (int i = 0; i < length; i++)
            {
                tongso = tongso + PhieuXuatDCChiTietBOCollection[i].SoLuong.Value;
                thanhtien = thanhtien + PhieuXuatDCChiTietBOCollection[i].ThanhTien.Value;
            }

            spinEditTongSo.Value = (decimal)tongso;
            spinEditThanhTien.Value = (decimal)thanhtien;

            double mucVAT = StaticUtil.GetDouble(spinEditVAT.Value, 0);
            double tienVAT = thanhtien * mucVAT;
            spinEditTienVAT.Value = (decimal)tienVAT;
            double mucCK = StaticUtil.GetDouble(spinEditChietKhau.Value, 0);
            double tienCK = thanhtien * mucCK;
            spinEditTienChietKhau.Value = (decimal)tienCK;
            double mucGG = StaticUtil.GetDouble(spinEditDungCumDungCu.Value, 0);
            double tienGG = thanhtien * mucGG;
            spinEditTienDungCumDungCu.Value = (decimal)tienGG;
            double TienPhatSinh = StaticUtil.GetDouble(spinEditTienPhatSinh.Value, 0);

            tongtien = thanhtien + tienVAT + TienPhatSinh + tienCK - tienGG;
            spinEditTongTien.Value = (decimal)tongtien;
            txtBangChu.Text = VNCurrency.ToString(tongtien);
        }

        private void DeleteDungCu(int DungCuID, int donvitinhID)
        {

            if (MessageBox.Show("Bạn có muốn xóa Mặt Hàng và Đơn vị tính này không? ", "Cảnh báo!!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                int _idPhieuXuatDC = StaticUtil.GetInt(this.grdViewPhieuXuatDCChiTiet.GetFocusedRowCellValue("PhieuXuatDCChiTietID"), -1);

                if (_idPhieuXuatDC >= 0)
                {
                    PhieuXuatDCChiTietBO.DeletePhieuXuatDCChiTietBO(_idPhieuXuatDC);
                }
                //PhieuXuatDCChiTietBOCollection.Remove(DungCuID,donvitinhID);
                sapxep();
            }
        }

        private int kiemtraLuu()
        {
            int tempkiemtra = 1;
            string strError = "";

            if (lookUpEditNhaCungCap.EditValue == null)
            {
                tempkiemtra = 0;
                strError = "Chưa chọn Khách hàng";
            }

            if (txtMaSo.Text.Trim() == "")
            {
                tempkiemtra = 0;
                strError = strError + " ;Chưa nhập Mã số";
            }

            if (txtChungTu.Text.Trim() == "")
            {
                tempkiemtra = 0;
                strError = strError + " ;Chưa nhập Chứng từ";
            }

            if (tempkiemtra == 0)
                MessageBox.Show(strError, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return tempkiemtra;
        }



        #endregion function

        private void toolStripMenuItemNhapKho_Click(object sender, EventArgs e)
        {
            int _PhieuXuatDCID = StaticUtil.GetInt(this.PhieuXuatDCBO.PhieuXuatDCID, -1);
            if (_PhieuXuatDCID >= 0 && this.PhieuXuatDCBO != null)
            {
                FrmXuatKhoDC2 frmXuatKhoDC2 = new FrmXuatKhoDC2((FrmMain)this.ParentForm, 0, this.PhieuXuatDCBO, this.PhieuXuatDCChiTietBOCollection);
                frmXuatKhoDC2.OnCompleted += new OnCompletedEventHandler(frmXuatKhoDC2_OnCompleted);
                frmXuatKhoDC2.Show();
            }
            else
                MessageBox.Show("Chưa chọn Hóa đơn Bán hàng để Thu tiền! ", "Thông báo", MessageBoxButtons.OK);
        }

        private void frmXuatKhoDC2_OnCompleted(object sender, OnCompletedEventArgs e)
        {
            int daluu = (int)e.objectValue;
            if (daluu == 1)
                RefreshData();

            this.BringToFront();
        }
    }
}